import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import LoginVue from "@/views/loginVue.vue";
import HomeView from '../views/homeVue.vue'
import TranslationPage from '../views/translationPage.vue'
import TerminologyPage from '../views/terminologyPage.vue'
import HistoryPage from '../views/historyPage.vue'
import NavigationPage from '../views/navigationPage.vue'
import LisuMedicinePage from '../views/lisuMedicinePage.vue'
import AboutUsPage from "@/views/aboutUsPage.vue";
import FeedbackPage from "@/views/feedback.vue"
import LacquerTreePage from "@/views/lacquer-treePage.vue";


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
      { path : '/login', name: 'Login', component: LoginVue, meta: { public: true } },
      { path: '/', redirect: '/home' },
      { path: '/home', name: 'Home', component: HomeView },
      { path: '/translation', name: 'Translation', component: TranslationPage },
      { path: '/terminology', name: 'Terminology', component: TerminologyPage },
      { path: '/history', name: 'History', component: HistoryPage },
      { path: '/navigation', name: 'Navigation', component: NavigationPage },
      { path: '/lisu-medicine', name: 'LisuMedicine', component: LisuMedicinePage },
      { path: '/about-us', name: 'AboutUs', component: AboutUsPage },
      { path: '/feedback', name: 'Feedback', component: FeedbackPage },
      { path: '/lacquer-tree', name: 'LacquerTree', component: LacquerTreePage }
  ],
})

router.beforeEach((to) => {
  const auth = useAuthStore()
  if (to.meta.public) return true
  if (!auth.isAuthenticated) {
    return { name: 'Login', query: { redirect: to.fullPath } }
  }
  return true
})

export default router
